package logic.util;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.ciko.code.bean.UserBean;
import com.shwread.android.qysw10000136.R;

import java.util.List;


public class PopupWindowUtil
{
	private Context             context;
	private List<UserBean>      data;
	private OnItemClickListener listener;
	
	public PopupWindowUtil(Context context, List<UserBean> data, OnItemClickListener listener) {
		super();
		this.context = context;
		this.data = data;
		this.listener = listener;
	}
	
	public View getView() {
		View view = LayoutInflater.from(context).inflate(R.layout.login_change_account_dialog, null);
		ListView changeListView = (ListView) view.findViewById(R.id.login_change_account_listview);
		changeListView.setOnItemClickListener(listener);
		changeListView.setFocusable(true);
		changeListView.setFocusableInTouchMode(true);
		changeListView.setDivider(new ColorDrawable(Color.GRAY));
		changeListView.setCacheColorHint(Color.TRANSPARENT);
		changeListView.setDividerHeight(1);
		Adapter mAdapter = new Adapter();
		changeListView.setAdapter(mAdapter);
		mAdapter.notifyDataSetChanged();
		return view;
	}
	
	class Adapter
	        extends
	        BaseAdapter
	{
		
		@Override
		public int getCount() {
			return data == null || data.isEmpty() ? 0 : (data.size()>5 ? 5 : data.size() );
		}
		
		@Override
		public String getItem(int position) {
			return data == null || data.isEmpty() ? "" : data.get(position).getAccount();
		}
		
		@Override
		public long getItemId(int position) {
			return position;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			DeviceViewHolder viewHolder = null;
			if (convertView == null)
			{
				viewHolder = new DeviceViewHolder();
				convertView = LayoutInflater.from(context).inflate(R.layout.ticket_account_item, null);
				viewHolder.name = (TextView) convertView.findViewById(R.id.ticket_account_tv);
				convertView.setTag(viewHolder);
			}
			else viewHolder = (DeviceViewHolder) convertView.getTag();
			viewHolder.name.setText(getItem(position));
			return convertView;
		}
		
		class DeviceViewHolder
		{
			public TextView  name;
		}
	}
}
